﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.Service.Entities;
using System.Web.Security;
using System.Web;

namespace HPIT.RentHouse.Service
{
    public class LoginService : ILogin
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public AjaxResult Login(LoginDTO dto)
        {
            var db = new RentEntity();
            var bs = new BaseService<T_AdminUsers>(db);
            //根据手机号查询用户是否存在
            var user = bs.Get(a=>a.PhoneNum==dto.PhoneNum);
            if (user==null)
            {
                return new AjaxResult(ResultState.Error,"用户名不存在！");
            }
            else
            {
                //判断密码是否正确
                var salt = user.PasswordSalt; //数据库中保存的盐值
                var pwd = CommonHelper.CalcMD5(dto.Password+salt);//用户在前台输入的密码
                if (pwd!=user.PasswordHash)
                {
                    return new AjaxResult(ResultState.Error,"密码错误！");
                }
                else
                {
                    //登录成功 缓存信息
                    var admin = new LoginSaveInfo();
                    admin.CityId=user.CityId==null?0:(long)user.CityId;
                    admin.Id=user.Id;   
                    admin.Name=user.Name;   
                    admin.PhoneNum=user.PhoneNum;
                    admin.RoleName = user.T_Roles.Count > 0 ?
                    user.T_Roles.FirstOrDefault().Name : "";
                    SaverData(admin, dto.IsRemeberme);

                    return new AjaxResult(ResultState.Success, "登录成功！");
                }
            }
        }

        /// <summary>
        /// 缓存用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <param name="isRemeberMe"></param>
        public void SaverData(LoginSaveInfo user, bool isRemeberMe)
        {
            //1、序列化要保存的用户信息
            var data = user.ToJson();

            //2、创建一个FormsAuthenticationTicket，它包含登录名以及额外的用户数据。
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, user.Name, DateTime.Now, DateTime.Now.AddDays(1), true, data);

            //3、加密保存
            string cookieValue = FormsAuthentication.Encrypt(ticket);

            // 4. 根据加密结果创建登录Cookie
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue);
            cookie.HttpOnly = true;
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Domain = FormsAuthentication.CookieDomain;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            //若用户勾选记得我，则指定cookie有效期（ Expires ）
            if (isRemeberMe)
            {
                cookie.Expires = DateTime.Now.AddDays(1);
            }

            // 5. 写登录Cookie（获取http请求上下文HttpContext ，移除原来的数据，存入新的）
            HttpContext.Current.Response.Cookies.Remove(cookie.Name);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}
